<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/img/iconfont.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/img/iconfont.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/img/iconfont.png">
  <link rel="mask-icon" href="/img/iconfont.png" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wang-weijun.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.26.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.json","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="交互机器人">
<meta property="og:type" content="article">
<meta property="og:title" content="Jarvis助手">
<meta property="og:url" content="http://wang-weijun.github.io/2023/05/17/Jarvis%E5%8A%A9%E6%89%8B/index.html">
<meta property="og:site_name" content="Phils的杂货铺">
<meta property="og:description" content="交互机器人">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wang-weijun.github.io/images/image-20230526201313108.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/image-20230513200145473.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20190904000333393.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NyaW1zb25L,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2019090400183484.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NyaW1zb25L,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20190904001142696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NyaW1zb25L,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2023-05-17T08:03:39.000Z">
<meta property="article:modified_time" content="2025-11-21T08:58:26.673Z">
<meta property="article:author" content="Phils">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wang-weijun.github.io/images/image-20230526201313108.png">


<link rel="canonical" href="http://wang-weijun.github.io/2023/05/17/Jarvis%E5%8A%A9%E6%89%8B/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://wang-weijun.github.io/2023/05/17/Jarvis%E5%8A%A9%E6%89%8B/","path":"2023/05/17/Jarvis助手/","title":"Jarvis助手"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Jarvis助手 | Phils的杂货铺</title>
  








  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.5.0/search.js" integrity="sha256-xFC6PJ82SL9b3WkGjFavNiA9gm5z6UBxWPiu4CYjptg=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>







  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Phils的杂货铺</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#ChatGPT"><span class="nav-number">1.</span> <span class="nav-text">ChatGPT</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#python%E8%B0%83%E7%94%A8openai%E5%BA%93"><span class="nav-number">1.1.</span> <span class="nav-text">python调用openai库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E8%BF%94%E5%9B%9E"><span class="nav-number">1.2.</span> <span class="nav-text">字节流返回</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%91%E5%87%BD%E6%95%B0"><span class="nav-number">1.3.</span> <span class="nav-text">云函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A0%E5%85%A5%E8%AF%AD%E9%9F%B3%E6%9C%8D%E5%8A%A1"><span class="nav-number">2.</span> <span class="nav-text">加入语音服务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%AD%E9%9F%B3%E4%BA%A4%E4%BA%92"><span class="nav-number">3.</span> <span class="nav-text">语音交互</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB"><span class="nav-number">4.</span> <span class="nav-text">人脸识别</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AF%B9%E5%8D%95%E4%B8%AA%E5%9B%BE%E7%89%87%E8%AF%86%E5%88%AB"><span class="nav-number">4.1.</span> <span class="nav-text">对单个图片识别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B0%83%E7%94%A8%E7%94%B5%E8%84%91%E6%91%84%E5%83%8F%E5%A4%B4%E8%AF%86%E5%88%AB"><span class="nav-number">4.2.</span> <span class="nav-text">调用电脑摄像头识别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%91%84%E5%83%8F%E5%A4%B4%E5%AE%9E%E6%97%B6%E5%BE%AE%E7%AC%91%E8%AF%86%E5%88%AB"><span class="nav-number">4.3.</span> <span class="nav-text">摄像头实时微笑识别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%80%E9%99%90%E6%80%A7"><span class="nav-number">4.4.</span> <span class="nav-text">局限性</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Phils"
      src="/img/profile.png">
  <p class="site-author-name" itemprop="name">Phils</p>
  <div class="site-description" itemprop="description">个人博客，IT，技术分享</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">40</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">40</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wang-weijun" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wang-weijun" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1191206969@qq.com" title="E-Mail → mailto:1191206969@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://wang-weijun.github.io/2023/05/17/Jarvis%E5%8A%A9%E6%89%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/img/profile.png">
      <meta itemprop="name" content="Phils">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Phils的杂货铺">
      <meta itemprop="description" content="个人博客，IT，技术分享">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Jarvis助手 | Phils的杂货铺">
      <meta itemprop="description" content="交互机器人">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Jarvis助手
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-17 16:03:39" itemprop="dateCreated datePublished" datetime="2023-05-17T16:03:39+08:00">2023-05-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-11-21 16:58:26" itemprop="dateModified" datetime="2025-11-21T16:58:26+08:00">2025-11-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E9%A1%B9%E7%9B%AE/" itemprop="url" rel="index"><span itemprop="name">项目</span></a>
        </span>
    </span>

  
</div>

            <div class="post-description">交互机器人</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="ChatGPT"><a href="#ChatGPT" class="headerlink" title="ChatGPT"></a>ChatGPT</h2><p>使用<a target="_blank" rel="noopener" href="https://portal.azure.com/">Azure</a>的<a target="_blank" rel="noopener" href="https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/">Azure OpenAI</a>服务，可以选择openai库或者使用API的方式</p>
<h3 id="python调用openai库"><a href="#python调用openai库" class="headerlink" title="python调用openai库"></a>python调用openai库</h3><p>导入库并列出模型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> openai</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">from</span> num2words <span class="keyword">import</span> num2words</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> openai.embeddings_utils <span class="keyword">import</span> get_embedding, cosine_similarity</span><br><span class="line"><span class="keyword">import</span> tiktoken</span><br><span class="line"></span><br><span class="line">API_KEY = os.getenv(<span class="string">&quot;AZURE_OPENAI_API_KEY&quot;</span>) </span><br><span class="line">RESOURCE_ENDPOINT = os.getenv(<span class="string">&quot;AZURE_OPENAI_ENDPOINT&quot;</span>) </span><br><span class="line"></span><br><span class="line">openai.api_type = <span class="string">&quot;azure&quot;</span></span><br><span class="line">openai.api_key = API_KEY</span><br><span class="line">openai.api_base = RESOURCE_ENDPOINT</span><br><span class="line">openai.api_version = <span class="string">&quot;2023-05-15&quot;</span></span><br><span class="line"></span><br><span class="line">url = openai.api_base + <span class="string">&quot;/openai/deployments?api-version=2023-05-15&quot;</span> </span><br><span class="line"></span><br><span class="line">r = requests.get(url, headers=&#123;<span class="string">&quot;api-key&quot;</span>: API_KEY&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(r.text)</span><br></pre></td></tr></table></figure>

<p>封装成类，新建<code>chat.py</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> openai</span><br><span class="line"><span class="keyword">import</span> tiktoken</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Chat_service</span>:</span><br><span class="line">    prompt = <span class="string">&quot;&quot;</span></span><br><span class="line">    template = <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            person: &#123;&#125;</span></span><br><span class="line"><span class="string">            AI:&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="comment"># OpenAI API</span></span><br><span class="line">        openai.api_type = <span class="string">&quot;azure&quot;</span></span><br><span class="line">        openai.api_key = API_KEY</span><br><span class="line">        openai.api_base = RESOURCE_ENDPOINT</span><br><span class="line">        openai.api_version = <span class="string">&quot;2023-05-15&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">num_tokens_from_string</span>(<span class="params">self, string: <span class="built_in">str</span>, encoding_name: <span class="built_in">str</span></span>) -&gt; <span class="built_in">int</span>:</span><br><span class="line">        <span class="string">&quot;&quot;&quot;Returns the number of tokens in a text string.&quot;&quot;&quot;</span></span><br><span class="line">        encoding = tiktoken.get_encoding(encoding_name)</span><br><span class="line">        num_tokens = <span class="built_in">len</span>(encoding.encode(string))</span><br><span class="line">        <span class="keyword">return</span> num_tokens</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">receive</span>(<span class="params">self, question, choose=<span class="number">0</span></span>):</span><br><span class="line">        filedir = os.path.dirname(os.path.realpath(__file__))</span><br><span class="line">        <span class="built_in">list</span> = &#123;<span class="number">0</span>: <span class="string">&#x27;/prompts/prompts.txt&#x27;</span>,</span><br><span class="line">                <span class="number">1</span>: <span class="string">&#x27;/prompts/开发者模式.txt&#x27;</span>,</span><br><span class="line">                <span class="number">2</span>: <span class="string">&#x27;/prompts/派蒙.txt&#x27;</span>,</span><br><span class="line">                <span class="number">3</span>: <span class="string">&#x27;/prompts/深度学习.txt&#x27;</span>,</span><br><span class="line">                <span class="number">4</span>: <span class="string">&#x27;/prompts/文字探险.txt&#x27;</span></span><br><span class="line">                &#125;</span><br><span class="line">        <span class="comment"># 加入提示词</span></span><br><span class="line">        file_url = filedir + <span class="built_in">list</span>[choose]</span><br><span class="line">        <span class="keyword">if</span> os.path.exists(file_url):</span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(file_url, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                <span class="variable language_">self</span>.prompt = f.read()</span><br><span class="line">        prompt_local = <span class="variable language_">self</span>.prompt + <span class="variable language_">self</span>.template.<span class="built_in">format</span>(question)</span><br><span class="line">        result = openai.Completion.create(</span><br><span class="line">            engine=<span class="string">&quot;text-davinci-003&quot;</span>,  <span class="comment"># “text-davinci-003” 模型用于文本生成，“code-davinci-002”模型用于代码生成</span></span><br><span class="line">            prompt=prompt_local,</span><br><span class="line">            temperature=<span class="number">0.9</span>,</span><br><span class="line">            max_tokens=<span class="number">1000</span>,  <span class="comment"># 4097是openai的限制，</span></span><br><span class="line">            stop=[<span class="string">&quot;person&quot;</span>, <span class="string">&quot;AI&quot;</span>, <span class="string">&quot;\n&quot;</span>],</span><br><span class="line">            top_p=<span class="number">1.0</span>,</span><br><span class="line">            frequency_penalty=<span class="number">0.0</span>,</span><br><span class="line">            presence_penalty=-<span class="number">0.6</span></span><br><span class="line">        )</span><br><span class="line">        text = result.choices[<span class="number">0</span>][<span class="string">&#x27;text&#x27;</span>]</span><br><span class="line">        <span class="keyword">return</span> text</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    cs = Chat_service()</span><br><span class="line">    n = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入模式（0:默认、1:开发者模式、2:派蒙、3:深度学习、4:文字探险）：&quot;</span>))</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        q = <span class="built_in">input</span>(<span class="string">&quot;Person：&quot;</span>)</span><br><span class="line">        <span class="keyword">if</span> q == <span class="string">&#x27;退出&#x27;</span>:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">        <span class="built_in">print</span>(cs.receive(q, n))</span><br></pre></td></tr></table></figure>

<h3 id="字节流返回"><a href="#字节流返回" class="headerlink" title="字节流返回"></a>字节流返回</h3><p>（stream&#x3D;True）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> openai</span><br><span class="line"></span><br><span class="line">openai.api_type = <span class="string">&quot;azure&quot;</span></span><br><span class="line">openai.api_base = &#123;终结点&#125;</span><br><span class="line">openai.api_version = <span class="string">&quot;2023-05-15&quot;</span></span><br><span class="line">openai.api_key = &#123;密钥&#125;</span><br><span class="line"></span><br><span class="line">template = <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        person: &#123;什么是resnet18&#125;</span></span><br><span class="line"><span class="string">        AI:&quot;&quot;&quot;</span></span><br><span class="line">response = openai.Completion.create(</span><br><span class="line">    engine=<span class="string">&quot;text-davinci-003&quot;</span>,</span><br><span class="line">    prompt=template,</span><br><span class="line">    temperature=<span class="number">0.9</span>,</span><br><span class="line">    max_tokens=<span class="number">256</span>,</span><br><span class="line">    top_p=<span class="number">1</span>,</span><br><span class="line">    frequency_penalty=<span class="number">0</span>,</span><br><span class="line">    presence_penalty=<span class="number">0</span>,</span><br><span class="line">    best_of=<span class="number">1</span>,</span><br><span class="line">    stream=<span class="literal">True</span>,</span><br><span class="line">    stop=[<span class="string">&quot;Human:&quot;</span>, <span class="string">&quot;AI:&quot;</span>])</span><br><span class="line">collected_events = []</span><br><span class="line">completion_text = <span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="comment"># iterate through the stream of events</span></span><br><span class="line"><span class="keyword">for</span> event <span class="keyword">in</span> response:</span><br><span class="line">    collected_events.append(event)  <span class="comment"># save the event response</span></span><br><span class="line">    event_text = event[<span class="string">&#x27;choices&#x27;</span>][<span class="number">0</span>][<span class="string">&#x27;text&#x27;</span>]  <span class="comment"># extract the text</span></span><br><span class="line">    completion_text += event_text  <span class="comment"># append the text</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;Text received: <span class="subst">&#123;event_text&#125;</span>&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;Full text received: <span class="subst">&#123;completion_text&#125;</span>&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>结果演示：</p>
<p><img src="/../images/image-20230526201313108.png" alt="image-20230526201313108"></p>
<p>xxxxxxxxxx Object massage &#x3D; redisUtil.lGetIndex(“sensor”, 0);JSONObject jsonObject &#x3D; JSON.parseObject(massage.toString());String sn &#x3D; jsonObject.getString(“SN”);BigDecimal temp &#x3D; jsonObject.getBigDecimal(“Temp”);BigDecimal humidity &#x3D; jsonObject.getBigDecimal(“Humidity”);String time &#x3D; jsonObject.getString(“time”);java</p>
<p>也可以使用openai的密钥，在腾讯云上创建云函数，挂载到美国，可直连接口。:arrow_down:</p>
<h3 id="云函数"><a href="#云函数" class="headerlink" title="云函数"></a>云函数</h3><p>app.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask,request</span><br><span class="line"></span><br><span class="line">opeailoaded=<span class="string">&quot;0&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">   <span class="keyword">import</span> openai</span><br><span class="line">   openai.api_key = <span class="string">&#x27;sk-xxxxxxxxxxxxxxxxxxxx&#x27;</span></span><br><span class="line">   opeailoaded=<span class="string">&quot;1&quot;</span></span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">   opeailoaded=<span class="string">&quot;0&quot;</span></span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">&#x27;/&#x27;</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">hello_world</span>():</span><br><span class="line">   <span class="keyword">if</span> opeailoaded==<span class="string">&quot;1&quot;</span>:</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&#x27;opeailoaded&#x27;</span></span><br><span class="line">   <span class="keyword">else</span>:</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&quot;openai not loaded&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route(<span class="params"><span class="string">&#x27;/ask&#x27;</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">ask</span>():</span><br><span class="line">   <span class="keyword">try</span>:</span><br><span class="line">      query=request.args.get(<span class="string">&quot;query&quot;</span>)</span><br><span class="line">      response=openai.ChatCompletion.create(</span><br><span class="line">         model=<span class="string">&quot;gpt-3.5-turbo&quot;</span>,</span><br><span class="line">         messages=[</span><br><span class="line">            &#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;system&quot;</span>, <span class="string">&quot;content&quot;</span>: <span class="string">&quot;You are a helpful assistant.&quot;</span>&#125;,</span><br><span class="line">            &#123;<span class="string">&quot;role&quot;</span>: <span class="string">&quot;user&quot;</span>, <span class="string">&quot;content&quot;</span>: query&#125;</span><br><span class="line">         ]</span><br><span class="line">      )</span><br><span class="line">      <span class="keyword">return</span> response [<span class="string">&#x27;choices&#x27;</span>][<span class="number">0</span>][<span class="string">&#x27;message&#x27;</span>][<span class="string">&#x27;content&#x27;</span>]</span><br><span class="line">   <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">      <span class="built_in">print</span>(<span class="built_in">str</span>(e))</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&quot;无法获取结果&quot;</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">   app.run(host=<span class="string">&#x27;0.0.0.0&#x27;</span>,port=<span class="number">9000</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>点击查看-终端</p>
<p><img src="/../images/image-20230513200145473.png" alt="image-20230513200145473"></p>
<p>输入安装 openai 库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> src <span class="comment"># 进入源代码库</span></span><br><span class="line">pip3 install openai -t. <span class="comment"># 安装到源代码路径下</span></span><br></pre></td></tr></table></figure>

<p>点击部署</p>
<h2 id="加入语音服务"><a href="#加入语音服务" class="headerlink" title="加入语音服务"></a>加入语音服务</h2><p>使用Azure的<a target="_blank" rel="noopener" href="https://azure.microsoft.com/zh-cn/products/cognitive-services/speech-services/">认知语音服务</a>，可文字转语音，语音转文字，也可以选择不同的声音。</p>
<p>封装成类，新建<code>voice_service.py</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> azure.cognitiveservices.speech <span class="keyword">as</span> speechsdk</span><br><span class="line"><span class="keyword">import</span> keyboard</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">VoiceService</span>:</span><br><span class="line">    text = <span class="string">&#x27;&#x27;</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        speech_key = &#123;密钥&#125;</span><br><span class="line">        service_region = &#123;新建服务时的地区&#125;</span><br><span class="line">        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)</span><br><span class="line">        audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=<span class="literal">True</span>)</span><br><span class="line">        speech_config.speech_synthesis_language = <span class="string">&quot;zh-CN&quot;</span></span><br><span class="line">        speech_config.speech_synthesis_voice_name =<span class="string">&quot;zh-CN-XiaoyanNeural&quot;</span> <span class="comment"># 女：zh-CN-XiaomoNeural 男：zh-CN-XiaoyanNeural</span></span><br><span class="line">        <span class="variable language_">self</span>.speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)</span><br><span class="line">        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)</span><br><span class="line">        <span class="variable language_">self</span>.speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, language=<span class="string">&quot;zh-CN&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">speak_text</span>(<span class="params">self, text</span>):</span><br><span class="line">        <span class="variable language_">self</span>.text = text</span><br><span class="line">        <span class="keyword">if</span> text != <span class="string">&#x27;&#x27;</span>:</span><br><span class="line">            <span class="variable language_">self</span>.speech_synthesizer.speak_text_async(text).get()</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="variable language_">self</span>.speech_synthesizer.speak_text_async(<span class="string">&#x27;我好像出错了，你能再说一遍吗？&#x27;</span>).get()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">speak</span>(<span class="params">self, text</span>):</span><br><span class="line">        <span class="variable language_">self</span>.speak_text(text)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">listen</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;按下空格键开始监听&quot;</span>)</span><br><span class="line">        keyboard.wait(<span class="string">&#x27;space&#x27;</span>)  <span class="comment"># 等待空格键按下</span></span><br><span class="line">        result = <span class="variable language_">self</span>.speech_recognizer.recognize_once_async().get()</span><br><span class="line">        <span class="keyword">return</span> result.text</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">listen_and_speak</span>(<span class="params">self</span>):</span><br><span class="line">        text = <span class="variable language_">self</span>.listen()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;问： <span class="subst">&#123;text&#125;</span>&#x27;</span>)</span><br><span class="line">        <span class="keyword">if</span> <span class="string">&#x27;再见&#x27;</span> <span class="keyword">in</span> text:</span><br><span class="line">            text = <span class="string">&#x27;再见，祝您生活愉快&#x27;</span></span><br><span class="line">            <span class="variable language_">self</span>.speak_text(text)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="variable language_">self</span>.speak_text(text)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&#x27;答： <span class="subst">&#123;text&#125;</span>&#x27;</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">True</span></span><br></pre></td></tr></table></figure>

<h2 id="语音交互"><a href="#语音交互" class="headerlink" title="语音交互"></a>语音交互</h2><p>首先将上面两个类保存下来，导入。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">from</span> chat <span class="keyword">import</span> Chat_service</span><br><span class="line"><span class="keyword">from</span> voice_service <span class="keyword">import</span> VoiceService</span><br><span class="line"></span><br><span class="line">vs = VoiceService()</span><br><span class="line">cs = Chat_service()</span><br><span class="line">n = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">text_chat</span>(<span class="params">n</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;有什么需要我帮助的吗？&#x27;</span>)</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            q = <span class="built_in">input</span>(<span class="string">&#x27;问：&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span> q == <span class="string">&#x27;退出&#x27;</span>:</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            response = cs.receive(q, n)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&#x27;AI：&#x27;</span> + response)</span><br><span class="line">            response = re.sub(<span class="string">r&#x27;\(.*?\)&#x27;</span>, <span class="string">&#x27;&#x27;</span>, response)</span><br><span class="line">            vs.speak(response)</span><br><span class="line">        <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">            <span class="built_in">print</span>(e)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">voice_chat</span>(<span class="params">n</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;我是一个智能机器人，我可以回答你的问题。&#x27;</span>)</span><br><span class="line">    vs.speak(<span class="string">&#x27;有什么需要我帮助的吗？&#x27;</span>)</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            q = vs.listen()</span><br><span class="line">            <span class="keyword">if</span> q <span class="keyword">in</span> <span class="string">&#x27;退出&#x27;</span>:</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            response = cs.receive(q, n)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&#x27;AI：&#x27;</span> + response)</span><br><span class="line">            response = re.sub(<span class="string">r&#x27;\(.*?\)&#x27;</span>, <span class="string">&#x27;&#x27;</span>, response)</span><br><span class="line">            vs.speak(response)</span><br><span class="line">        <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">            <span class="built_in">print</span>(e)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    choose = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;选择模式(1.文字聊天，2.语音聊天)：&#x27;</span>))</span><br><span class="line">    n = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;选择模式(0.默认，1.开发者模式，2.派蒙，3.深度学习，4.文字探险)：&#x27;</span>))</span><br><span class="line">    <span class="keyword">if</span> choose == <span class="number">1</span>:</span><br><span class="line">        text_chat(n)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        voice_chat(n)</span><br></pre></td></tr></table></figure>



<h2 id="人脸识别"><a href="#人脸识别" class="headerlink" title="人脸识别"></a>人脸识别</h2><p><strong>本文内容</strong><br>1.使用基于Haar特征的Cascade级联分类器进行人脸识别（听起来好高大上，但其实原理很简单）<br>2.用人脸识别同样的道理，扩展到人眼识别上<br>3.用opencv自带的Harr级联分类器进行人脸、人眼与微笑识别</p>
<p><strong>什么是Harr特征</strong></p>
<p>Haar特征包含三种：边缘特征、线性特征、中心特征和对角线特征。每种分类器都从图片中提取出对应的特征。</p>
<p><strong>什么是Cascade级联分类器</strong><br>基于Haar特征的cascade级联分类器是Paul Viola和 Michael Jone在2001年的论文”Rapid Object Detection using a Boosted Cascade of Simple Features”中提出的一种有效的物体检测方法。</p>
<p><strong>Cascade级联分类器的训练方法：Adaboost</strong><br>级联分类器的函数是通过大量带人脸和不带人脸的图片通过机器学习得到的。对于人脸识别来说，需要几万个特征，通过机器学习找出人脸分类效果最好、错误率最小的特征。训练开始时，所有训练集中的图片具有相同的权重，对于被分类错误的图片，提升权重，重新计算出新的错误率和新的权重。直到错误率或迭代次数达到要求。这种方法叫做Adaboost。</p>
<p>在Opencv中可以直接调用级联分类器函数。</p>
<p><strong>将弱分类器聚合成强分类器</strong><br>最终的分类器是这些弱分类器的加权和。之所以称之为弱分类器是因为每个分类器不能单独分类图片，但是将他们聚集起来就形成了强分类器。论文表明，只需要200个特征的分类器在检测中的精确度达到了95%。最终的分类器大约有6000个特征。(将超过160000个特征减小到6000个，这是非常大的进步了） 。</p>
<p><strong>级联的含义：需过五关斩六将才能被提取出来</strong><br>事实上，一张图片绝大部分的区域都不是人脸。如果对一张图片的每个角落都提取6000个特征，将会浪费巨量的计算资源。</p>
<p>如果能找到一个简单的方法能够检测某个窗口是不是人脸区域，如果该窗口不是人脸区域，那么就只看一眼便直接跳过，也就不用进行后续处理了，这样就能集中精力判别那些可能是人脸的区域。 为此，有人引入了Cascade 分类器。它不是将6000个特征都用在一个窗口，而是将特征分为不同的阶段，然后一个阶段一个阶段的应用这些特征(通常情况下，前几个阶段只有很少量的特征)。如果窗口在第一个阶段就检测失败了，那么就直接舍弃它，无需考虑剩下的特征。如果检测通过，则考虑第二阶段的特征并继续处理。如果所有阶段的都通过了，那么这个窗口就是人脸区域。 作者的检测器将6000+的特征分为了38个阶段，前五个阶段分别有1，10，25，25，50个特征(前文图中提到的识别眼睛和鼻梁的两个特征实际上是Adaboost中得到的最好的两个特征)。根据作者所述，平均每个子窗口只需要使用6000+个特征中的10个左右。</p>
<p><strong>OpenCV中的Haar-cascade检测</strong><br>OpenCV 既可以作为检测器也可以进行机器学习训练。如果你打算训练自己的分类器识别任意的物品，比如车，飞机，咖啡杯等。你可以用OpenCV 创造一个。完整的细节在:Cascade Classifier Training中。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 人脸识别</span></span><br><span class="line"><span class="comment"># 导入opencv-python</span></span><br><span class="line"><span class="keyword">import</span> cv2</span><br><span class="line"></span><br><span class="line"><span class="comment"># 读入一张图片，引号里为图片的路径，需要你自己手动设置</span></span><br><span class="line">img = cv2.imread(<span class="string">&#x27;image1.jpg&#x27;</span>,<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 导入人脸级联分类器引擎，&#x27;.xml&#x27;文件里包含训练出来的人脸特征</span></span><br><span class="line">face_engine = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_frontalface_default.xml&#x27;</span>)</span><br><span class="line"><span class="comment"># 用人脸级联分类器引擎进行人脸识别，返回的faces为人脸坐标列表，1.3是放大比例，5是重复识别次数</span></span><br><span class="line">faces = face_engine.detectMultiScale(img,scaleFactor=<span class="number">1.3</span>,minNeighbors=<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 对每一张脸，进行如下操作</span></span><br><span class="line"><span class="keyword">for</span> (x,y,w,h) <span class="keyword">in</span> faces:</span><br><span class="line">    <span class="comment"># 画出人脸框，蓝色（BGR色彩体系），画笔宽度为2</span></span><br><span class="line">    img = cv2.rectangle(img,(x,y),(x+w,y+h),(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>),<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 在&quot;img2&quot;窗口中展示效果图</span></span><br><span class="line">cv2.imshow(<span class="string">&#x27;img2&#x27;</span>,img)</span><br><span class="line"><span class="comment"># 监听键盘上任何按键，如有按键即退出并关闭窗口，并将图片保存为output.jpg</span></span><br><span class="line">cv2.waitKey(<span class="number">0</span>)</span><br><span class="line">cv2.destroyAllWindows()</span><br><span class="line">cv2.imwrite(<span class="string">&#x27;output.jpg&#x27;</span>,img)</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/20190904000333393.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NyaW1zb25L,size_16,color_FFFFFF,t_70" alt="img"></p>
<h3 id="对单个图片识别"><a href="#对单个图片识别" class="headerlink" title="对单个图片识别"></a>对单个图片识别</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 单张图片人脸+眼睛识别</span></span><br><span class="line"><span class="comment">#导入opencv</span></span><br><span class="line"><span class="keyword">import</span> cv2</span><br><span class="line"></span><br><span class="line"><span class="comment"># 导入人脸级联分类器引擎，&#x27;.xml&#x27;文件里包含训练出来的人脸特征，cv2.data.haarcascades即为存放所有级联分类器模型文件的目录</span></span><br><span class="line">face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_frontalface_default.xml&#x27;</span>)</span><br><span class="line"><span class="comment"># 导入人眼级联分类器引擎吗，&#x27;.xml&#x27;文件里包含训练出来的人眼特征</span></span><br><span class="line">eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_eye.xml&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 读入一张图片，引号里为图片的路径，需要你自己手动设置</span></span><br><span class="line">img = cv2.imread(<span class="string">&#x27;image3.png&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 用人脸级联分类器引擎进行人脸识别，返回的faces为人脸坐标列表，1.3是放大比例，5是重复识别次数</span></span><br><span class="line">faces = face_cascade.detectMultiScale(img, <span class="number">1.3</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 对每一张脸，进行如下操作</span></span><br><span class="line"><span class="keyword">for</span> (x,y,w,h) <span class="keyword">in</span> faces:</span><br><span class="line">    <span class="comment"># 画出人脸框，蓝色（BGR色彩体系），画笔宽度为2</span></span><br><span class="line">    img = cv2.rectangle(img,(x,y),(x+w,y+h),(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>),<span class="number">2</span>)</span><br><span class="line">    <span class="comment"># 框选出人脸区域，在人脸区域而不是全图中进行人眼检测，节省计算资源</span></span><br><span class="line">    face_area = img[y:y+h, x:x+w]</span><br><span class="line">    eyes = eye_cascade.detectMultiScale(face_area)</span><br><span class="line">    <span class="comment"># 用人眼级联分类器引擎在人脸区域进行人眼识别，返回的eyes为眼睛坐标列表</span></span><br><span class="line">    <span class="keyword">for</span> (ex,ey,ew,eh) <span class="keyword">in</span> eyes:</span><br><span class="line">        <span class="comment">#画出人眼框，绿色，画笔宽度为1</span></span><br><span class="line">        cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(<span class="number">0</span>,<span class="number">255</span>,<span class="number">0</span>),<span class="number">1</span>)</span><br><span class="line">        </span><br><span class="line"><span class="comment"># 在&quot;img2&quot;窗口中展示效果图</span></span><br><span class="line">cv2.imshow(<span class="string">&#x27;img2&#x27;</span>,img)</span><br><span class="line"><span class="comment"># 监听键盘上任何按键，如有案件即退出并关闭窗口，并将图片保存为output.jpg</span></span><br><span class="line">cv2.waitKey(<span class="number">0</span>)</span><br><span class="line">cv2.destroyAllWindows()</span><br><span class="line">cv2.imwrite(<span class="string">&#x27;output.jpg&#x27;</span>,img)</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/2019090400183484.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NyaW1zb25L,size_16,color_FFFFFF,t_70" alt="img"></p>
<h3 id="调用电脑摄像头识别"><a href="#调用电脑摄像头识别" class="headerlink" title="调用电脑摄像头识别"></a>调用电脑摄像头识别</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 调用电脑摄像头进行实时人脸+眼睛识别</span></span><br><span class="line"><span class="keyword">import</span> cv2</span><br><span class="line"></span><br><span class="line">face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_frontalface_default.xml&#x27;</span>)</span><br><span class="line"></span><br><span class="line">eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_eye.xml&#x27;</span>)</span><br><span class="line"><span class="comment"># 调用摄像头摄像头</span></span><br><span class="line">cap = cv2.VideoCapture(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="literal">True</span>):</span><br><span class="line">    <span class="comment"># 获取摄像头拍摄到的画面</span></span><br><span class="line">    ret, frame = cap.read()</span><br><span class="line">    faces = face_cascade.detectMultiScale(frame, <span class="number">1.3</span>, <span class="number">5</span>)</span><br><span class="line">    img = frame</span><br><span class="line">    <span class="keyword">for</span> (x,y,w,h) <span class="keyword">in</span> faces:</span><br><span class="line">    	<span class="comment"># 画出人脸框，蓝色，画笔宽度微</span></span><br><span class="line">        img = cv2.rectangle(img,(x,y),(x+w,y+h),(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>),<span class="number">2</span>)</span><br><span class="line">    	<span class="comment"># 框选出人脸区域，在人脸区域而不是全图中进行人眼检测，节省计算资源</span></span><br><span class="line">        face_area = img[y:y+h, x:x+w]</span><br><span class="line">        eyes = eye_cascade.detectMultiScale(face_area)</span><br><span class="line">    	<span class="comment"># 用人眼级联分类器引擎在人脸区域进行人眼识别，返回的eyes为眼睛坐标列表</span></span><br><span class="line">        <span class="keyword">for</span> (ex,ey,ew,eh) <span class="keyword">in</span> eyes:</span><br><span class="line">            <span class="comment">#画出人眼框，绿色，画笔宽度为1</span></span><br><span class="line">            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(<span class="number">0</span>,<span class="number">255</span>,<span class="number">0</span>),<span class="number">1</span>)</span><br><span class="line">        </span><br><span class="line">	<span class="comment"># 实时展示效果画面</span></span><br><span class="line">    cv2.imshow(<span class="string">&#x27;frame2&#x27;</span>,img)</span><br><span class="line">    <span class="comment"># 每5毫秒监听一次键盘动作</span></span><br><span class="line">    <span class="keyword">if</span> cv2.waitKey(<span class="number">5</span>) &amp; <span class="number">0xFF</span> == <span class="built_in">ord</span>(<span class="string">&#x27;q&#x27;</span>):</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 最后，关闭所有窗口</span></span><br><span class="line">cap.release()</span><br><span class="line">cv2.destroyAllWindows()</span><br></pre></td></tr></table></figure>

<h3 id="摄像头实时微笑识别"><a href="#摄像头实时微笑识别" class="headerlink" title="摄像头实时微笑识别"></a>摄像头实时微笑识别</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 调用电脑摄像头进行实时人脸+眼睛+微笑识别</span></span><br><span class="line"><span class="keyword">import</span> cv2</span><br><span class="line"></span><br><span class="line">face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_frontalface_default.xml&#x27;</span>)</span><br><span class="line"></span><br><span class="line">eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_eye.xml&#x27;</span>)</span><br><span class="line"></span><br><span class="line">smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+<span class="string">&#x27;haarcascade_smile.xml&#x27;</span>)</span><br><span class="line"><span class="comment"># 调用摄像头摄像头</span></span><br><span class="line">cap = cv2.VideoCapture(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="literal">True</span>):</span><br><span class="line">    <span class="comment"># 获取摄像头拍摄到的画面</span></span><br><span class="line">    ret, frame = cap.read()</span><br><span class="line">    faces = face_cascade.detectMultiScale(frame, <span class="number">1.3</span>, <span class="number">2</span>)</span><br><span class="line">    img = frame</span><br><span class="line">    <span class="keyword">for</span> (x,y,w,h) <span class="keyword">in</span> faces:</span><br><span class="line">    	<span class="comment"># 画出人脸框，蓝色，画笔宽度微</span></span><br><span class="line">        img = cv2.rectangle(img,(x,y),(x+w,y+h),(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>),<span class="number">2</span>)</span><br><span class="line">    	<span class="comment"># 框选出人脸区域，在人脸区域而不是全图中进行人眼检测，节省计算资源</span></span><br><span class="line">        face_area = img[y:y+h, x:x+w]</span><br><span class="line">        </span><br><span class="line">        <span class="comment">## 人眼检测</span></span><br><span class="line">        <span class="comment"># 用人眼级联分类器引擎在人脸区域进行人眼识别，返回的eyes为眼睛坐标列表</span></span><br><span class="line">        eyes = eye_cascade.detectMultiScale(face_area,<span class="number">1.3</span>,<span class="number">10</span>)</span><br><span class="line">        <span class="keyword">for</span> (ex,ey,ew,eh) <span class="keyword">in</span> eyes:</span><br><span class="line">            <span class="comment">#画出人眼框，绿色，画笔宽度为1</span></span><br><span class="line">            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(<span class="number">0</span>,<span class="number">255</span>,<span class="number">0</span>),<span class="number">1</span>)</span><br><span class="line">        </span><br><span class="line">        <span class="comment">## 微笑检测</span></span><br><span class="line">        <span class="comment"># 用微笑级联分类器引擎在人脸区域进行人眼识别，返回的eyes为眼睛坐标列表</span></span><br><span class="line">        smiles = smile_cascade.detectMultiScale(face_area,scaleFactor= <span class="number">1.16</span>,minNeighbors=<span class="number">65</span>,minSize=(<span class="number">25</span>, <span class="number">25</span>),flags=cv2.CASCADE_SCALE_IMAGE)</span><br><span class="line">        <span class="keyword">for</span> (ex,ey,ew,eh) <span class="keyword">in</span> smiles:</span><br><span class="line">            <span class="comment">#画出微笑框，红色（BGR色彩体系），画笔宽度为1</span></span><br><span class="line">            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(<span class="number">0</span>,<span class="number">0</span>,<span class="number">255</span>),<span class="number">1</span>)</span><br><span class="line">            cv2.putText(img,<span class="string">&#x27;Smile&#x27;</span>,(x,y-<span class="number">7</span>), <span class="number">3</span>, <span class="number">1.2</span>, (<span class="number">0</span>, <span class="number">0</span>, <span class="number">255</span>), <span class="number">2</span>, cv2.LINE_AA)</span><br><span class="line">        </span><br><span class="line">	<span class="comment"># 实时展示效果画面</span></span><br><span class="line">    cv2.imshow(<span class="string">&#x27;frame2&#x27;</span>,img)</span><br><span class="line">    <span class="comment"># 每5毫秒监听一次键盘动作</span></span><br><span class="line">    <span class="keyword">if</span> cv2.waitKey(<span class="number">5</span>) &amp; <span class="number">0xFF</span> == <span class="built_in">ord</span>(<span class="string">&#x27;q&#x27;</span>):</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 最后，关闭所有窗口</span></span><br><span class="line">cap.release()</span><br><span class="line">cv2.destroyAllWindows()</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/20190904001142696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NyaW1zb25L,size_16,color_FFFFFF,t_70" alt="img"></p>
<h3 id="局限性"><a href="#局限性" class="headerlink" title="局限性"></a>局限性</h3><p>1.仅为人脸检测，非人脸“辩识”，即只能框出人脸的位置，看不出人脸是谁。<br>2.仅能标出静态图片和视频帧上的人脸、人眼和微笑，不能进行“活体识别”，即不能看出这张脸是真人还是手机上的照片，如果用于人脸打卡签到、人脸支付的话会带来潜在的安全风险。<br>3.仅为普通的机器学习方法，没有用到深度学习和深层神经网络。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Python/" rel="tag"># Python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/05/17/Java%E5%B7%A5%E5%85%B7%E7%B1%BB/" rel="prev" title="Java工具类">
                  <i class="fa fa-angle-left"></i> Java工具类
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/05/17/Markdown%E8%AF%AD%E6%B3%95/" rel="next" title="Markdown语法">
                  Markdown语法 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Phils</span>
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
